/**
 * This is the main file of FMS Dating Application
 */

/**
 * @requires TraceUtils to trace complex objects
 */
load("TraceUtils.asc");

/**
 * Handles application start events.
 */
application.onAppStart = function(){
	trace("KNOWBANDLOBBY: Application started.");
	this.users = new Object();
	this.traceUtils = new TraceUtils();
	this.chatHistory = new Array();
}

/**
 * Handles application stop events.
 */
application.onAppStop = function () {
	trace("KNOWBANDLOBBY: Application stopped.");
}

application.onConnect = function(clientObj, userId, sessionId, userConfig) {
	
	// TODO: Check the user id and session data.
	
	// Is the user already connected?
	for(i = 0; i < this.clients.length; i++) {
	    if(this.clients[i].userId == userId) {
			trace("KNOWBANDLOBBY: User with the ID " + userId + " already connected. First connection will be closed.");
			this.clients[i].droppedUser = true;
			application.disconnect(this.clients[i]);
			break;
		}
	}
	
	trace("KNOWBANDLOBBY: User connected and accepted with the following data:");
	clientObj.userId = userId;
	clientObj.sessionId = sessionId;
	clientObj.userConfig = userConfig;
	trace(this.traceUtils.traceString(clientObj,"\tUser",1,""));
	// No problem with the user, accept it.
	application.acceptConnection(clientObj);
	
	// Broadcast this to other users...
	// We use a workaround of setInterval so that FMS broadcasts the message.
	application["connectTimerFor"+clientObj.userId] = setInterval(function (id, userConfig) {application.broadcastMsg("addUser", userConfig);clearInterval(application["connectTimerFor"+id])}, 100, clientObj.userId, clientObj.userConfig);
}

application.onDisconnect = function(clientObj){
	trace("KNOWBANDLOBBY: " + clientObj.userConfig.fullName + " disconnected.");
	if(clientObj.droppedUser != true) {
		application["disconnectTimerFor"+clientObj.userId] = setInterval(function (id) {application.broadcastMsg("removeUser", id);clearInterval(application["disconnectTimerFor"+id])}, 100, clientObj.userId);
	} else {
		trace("KNOWBANDLOBBY: Old connection of " + clientObj.userConfig.fullName + " is dropped.");
	}
}


application.onConnectReject = function(clientObj) {
	trace("KNOWBANDLOBBY: Client is rejected.");
}

Client.prototype.sendMessage = function (message) {
  trace("KNOWBANDLOBBY: " + this.userConfig.fullName + " says '" + message + "'.");
	application.broadcastMsg("getMessage", this.userId, this.userConfig, message);
	application.chatHistory.push({userId:this.userId, userConfig:this.userConfig, message:message});
}

Client.prototype.getUsers = function() {
  trace("KNOWBANDLOBBY: " + this.userConfig.fullName + " gets current users.");
  var users = new Array();
  for(var i = 0; i < application.clients.length; i++) {
    users[i] = application.clients[i].userConfig;
  }
  return users;
}

Client.prototype.getChatHistory = function() {
  trace("KNOWBANDLOBBY: " + this.userConfig.fullName + " gets chat history.");
  return application.chatHistory;
}

Client.prototype.clearChatHistory = function() {
  application.chatHistory = new Array();
}

Client.prototype.startSession = function () {
  trace("KNOWBANDLOBBY: Session is starting.");
  application.broadcastMsg("startSession");
}

Client.prototype.endLobbyApplication = function () {
  trace("KNOWBANDLOBBY: End Lobby Application.");
  setInterval(application.shutdown,3000);
}